package com.suning.sawp.service.impl.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.suning.framework.dal.client.DalClient;
import com.suning.sawp.dto.yxwt.BatchUpdateRevisitInfo;
import com.suning.sawp.dto.yxwt.CateInfo;
import com.suning.sawp.dto.yxwt.OrderStaffInfo;
import com.suning.sawp.dto.yxwt.RevisitInfo;
import com.suning.sawp.po.yxwt.SaledNoTake;
import com.suning.sawp.service.impl.yxwt.SaledNoTakeServiceImpl;

/**
 * 已销未提Dao
 *
 * @author 15050769
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("saledNoTakeDaoService")
public class SaledNoTakeDaoService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SaledNoTakeServiceImpl.class);

    @Resource
    DalClient dalClient;

    private final String YXWT_QUERYORDERLIST = "YXWT.QUERY_ORDERLIST";

    private final String YXWT_QUERYCATEINFOBYSTORE = "YXWT.QUERY_CATEINFOBYSTORE";

    private final String YXWT_QUERY_CATEINFOBYSTORE_ALL = "YXWT.QUERY_CATEINFOBYSTORE_ALL";

    private final String YXWT_QUERY_CATEINFOBYST_CA = "YXWT.YXWT_QUERY_CATEINFOBYST_CA";

    private final String YXWT_UPDATE_REVISIT = "YXWT.YXWT_UPDATE_REVISIT";

    private final String YXWT_QUERYORDERLISTBYSTATUS = "YXWT.QUERYORDERLISTBYSTATUS";

    private final String YXWT_QUERYORDERLISTBYSTATUS_PART = "YXWT.QUERYORDERLISTBYSTATUS_PART";

    private final String YXWT_QUERYORDERLIST_TOTAL = "YXWT.QUERYORDERLIST_TOTAL";

    private final String YXWT_QUERYORDERCOUNTBYSTATUS = "YXWT.QUERYORDERCOUNTBYSTATUS";

    private final String YXWT_QUERYORDERCOUNTBYSTATUS_TOTAL = "YXWT.QUERYORDERCOUNTBYSTATUS_TOTAL";

    private final String YXWT_QUERYORDERCOUNTBYCATA = "YXWT.QUERYORDERCOUNTBYCATA";
    
    private final String YXWT_QUERYCACHEINDEX = "YXWT.QUERYCACHEINDEX";

    /*
     * 已访问
     */
    private final String ACCESS_STATUS_VISITED = "0";

    /*
     * 未访问
     */
    private final String ACCESS_STATUS_NO_VISITED = "1";

    private final int UPDATE_FLAG = 1;

    /**
     * 
     * 功能描述: 根据工号和客户ID查询店员客户关系<br>
     * 〈功能详细描述〉
     *
     * @param staffId
     * @param accessStatus
     * @param custId
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<SaledNoTake> queryOrderList(int index, int size, String cateCode, String storeCode, String staffId,
            String accessStatus) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("index", index);
        paramMap.put("size", size);
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        } else if (StringUtils.isNotBlank(staffId)) {
            paramMap.put("staffId", staffId);
        }
        // 查询已经访问
        if (ACCESS_STATUS_VISITED.equals(accessStatus)) {
            paramMap.put("visitType", "-1");
        } else if (ACCESS_STATUS_NO_VISITED.equals(accessStatus)) {
            // 查询未访问
            paramMap.put("visitType", "0");
        }
        paramMap.put("storeCode", storeCode);
        List<SaledNoTake> resultList = dalClient.queryForList(YXWT_QUERYORDERLIST, paramMap, SaledNoTake.class);
        return resultList;
    }

    /**
     * 查询全品类维度按品类已销未提统计数据
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CateInfo> queryCateInfoByStore(String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        List<CateInfo> resultList = dalClient.queryForList(YXWT_QUERYCATEINFOBYSTORE, paramMap, CateInfo.class);
        return resultList;

    }

    /**
     * 查询店长所有品类维度已销未提的统计数据
     *
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CateInfo> queryCateInfoByStoreAll(String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        List<CateInfo> resultList = dalClient.queryForList(YXWT_QUERY_CATEINFOBYSTORE_ALL, paramMap, CateInfo.class);
        return resultList;
    }

    /**
     * 查询对应品类下的统计信息
     *
     * @param cateCode
     * @param storeCode
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<CateInfo> queryCateInfoByCateCode(String cateCode, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("storeCode", storeCode);
        paramMap.put("cateCode", cateCode);
        List<CateInfo> resultList = dalClient.queryForList(YXWT_QUERY_CATEINFOBYST_CA, paramMap, CateInfo.class);
        return resultList;
    }

    /**
     * 更新已销未提更新状态
     *
     * @param orderItemId
     * @param content
     * @param reVisitType
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public boolean updateRevisit(String orderItemId, String content, String reVisitType) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("orderItemId", orderItemId);
        paramMap.put("content", content);
        paramMap.put("reVisitType", reVisitType);
        int result = dalClient.execute(YXWT_UPDATE_REVISIT, paramMap);
        if (UPDATE_FLAG == result) {
            return true;
        }
        return false;
    }

    /**
     * 根据回访状态查询订单列表信息
     *
     * @param index
     * @param size
     * @param cateCode
     * @param storeCode
     * @param staffId
     * @param accessStatus
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public List<SaledNoTake> queryOrderListByStatus(int index, int size, String cateCode, String storeCode,
            String staffId, String accessStatus) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("index", index);
        paramMap.put("size", size);
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        } else if (StringUtils.isNotBlank(staffId)) {
            paramMap.put("staffId", staffId);
        }

        paramMap.put("storeCode", storeCode);
        List<SaledNoTake> resultList = new ArrayList<SaledNoTake>();
        if (SaledNoTake.AccConstants.TOTAL.equals(accessStatus)) {
            paramMap.put("staffId", staffId);
            resultList = dalClient.queryForList(YXWT_QUERYORDERLIST_TOTAL, paramMap, SaledNoTake.class);
        } else if (SaledNoTake.AccConstants.PART_BACK_CALLED.equals(accessStatus)) {
            resultList = dalClient.queryForList(YXWT_QUERYORDERLISTBYSTATUS_PART, paramMap, SaledNoTake.class);
        } else {
            if (StringUtils.isNotBlank(accessStatus)) {
                paramMap.put("visitType", accessStatus);
            }
            resultList = dalClient.queryForList(YXWT_QUERYORDERLISTBYSTATUS, paramMap, SaledNoTake.class);
        }
        return resultList;

    }

    /**
     * 根据回访状态查询对应的已销未提订单数量 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @param cateCode
     * @param storeCode
     * @param staffId
     * @param accessStatus
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public int queryOrderCountByStatus(String cateCode, String storeCode, String staffId, String accessStatus) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 如果用用类目查询则不用该店员的工号查询
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        } else if (StringUtils.isNotBlank(staffId)) {
            paramMap.put("staffId", staffId);
        }

        paramMap.put("storeCode", storeCode);
        int result = 0;
        if (SaledNoTake.AccConstants.PART_BACK_CALLED.equals(accessStatus)) {
            // 查询该次查询所有的订单数目
            int totalResult = dalClient.queryForObject(YXWT_QUERYORDERCOUNTBYSTATUS_TOTAL, paramMap, Integer.class);
            // 查询未访问客户的相关订单
            int result1 = queryPartOrderCountByStatus(cateCode, storeCode, staffId, "0");
            // 查询已确认的客户的相关订单
            int result2 = queryPartOrderCountByStatus(cateCode, storeCode, staffId, "1");
            // 相关数据相减就是对应的未确认的订单数目
            result = totalResult - result1 - result2;
        } else {
            result = queryPartOrderCountByStatus(cateCode, storeCode, staffId, accessStatus);
        }
        return result;
    }

    public int queryPartOrderCountByStatus(String cateCode, String storeCode, String staffId, String accessStatus) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 如果用用类目查询则不用该店员的工号查询
        if (StringUtils.isNotBlank(cateCode)) {
            paramMap.put("cateCode", cateCode);
        } else if (StringUtils.isNotBlank(staffId)) {
            paramMap.put("staffId", staffId);
        }
        paramMap.put("storeCode", storeCode);
        paramMap.put("visitType", accessStatus);
        int result = dalClient.queryForObject(YXWT_QUERYORDERCOUNTBYSTATUS, paramMap, Integer.class);
        return result;
    }

    @SuppressWarnings("unchecked")
    public boolean batchUpdateRevisit(BatchUpdateRevisitInfo batchUpdateRevisitInfo) {
        Map<String, Object>[] revisitInfoArr = Iterables.toArray(Iterables.transform(
                batchUpdateRevisitInfo.getMyOrderList(), new Function<RevisitInfo, Map<String, Object>>() {
                    @Override
                    public Map<String, Object> apply(RevisitInfo revisitInfo) {
                        Map<String, Object> paramMap = new HashMap<String, Object>();
                        paramMap.put("orderItemId", revisitInfo.getOrderItemId());
                        paramMap.put("content", revisitInfo.getReVisitContent());
                        paramMap.put("reVisitType", revisitInfo.getReVisitStatus());
                        return paramMap;
                    }
                }), Map.class);
        try {
            dalClient.batchUpdate(YXWT_UPDATE_REVISIT, revisitInfoArr);
        } catch (Exception e) {
            LOGGER.error(" batchUpdateRevisit batchUpdateRevisitInfo:" + revisitInfoArr, e);
            return false;
        }
        return true;
    }

    public List<OrderStaffInfo> queryOrderCountByCata(String cateCode, String storeCode) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        // 如果用用类目查询则不用该店员的工号查询
        paramMap.put("cateCode", cateCode);
        paramMap.put("storeCode", storeCode);
        List<OrderStaffInfo> result = dalClient
                .queryForList(YXWT_QUERYORDERCOUNTBYCATA, paramMap, OrderStaffInfo.class);
        return result;

    }

    public List<String> queryCacheIndex(String staffId) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("staffId", staffId);
        List<String> result = dalClient
                .queryForList(YXWT_QUERYCACHEINDEX, paramMap, String.class);
        return result;
    }

}
